﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ModuleDALCore;
using DTO;

namespace DAL
{
	public class THANHTOANDAO:DALBase
	{
		public THANHTOANDAO()
        {
            this.OpenConnect();
        }
        private DataTable dt;
        private string _SpName;

        public string SpName
        {
            get { return "SPTHANHTOAN"; }
            set { _SpName = value; }
        }
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
		public long INSERTHANHTOAN(THANHTOAN tt)
		{ 
			try
			{
				return long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 1)).ToString());
			}
			catch { return 0;}
		}
		public bool INSERTCT_THANHTOAN(CT_THANHTOAN ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_THANHTOAN", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 1)) == 1);
			}
			catch { return false; }
		}
		public long INSERT_PHIEUTHU(PHIEUTHU pt)
		{
			try
			{
				return long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction("SPPHIEUTHU", ConvertClassToArray.GetParamName(pt, null), ConvertClassToArray.GetValues(pt, 2)).ToString());
			}
			catch { return 0; }
		}
		public bool UPDATE_KHAMBENH(CT_KHAMBENH ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_KHAMBENH", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 8)) == 1);
			}
			catch { return false; }
		}
		public bool TRUCOC(TIENCOC tc)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENCOC", ConvertClassToArray.GetParamName(tc, null), ConvertClassToArray.GetValues(tc, 3)) == 1);
			}
			catch { return false; }
		}
		public bool UPDATE_IDPHIEUTHUTHANHTOAN(THANHTOAN tt)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 2)) == 1);
			}
			catch { return false; }
		}
		public bool INSERT(THANHTOAN tt, List<CT_THANHTOAN> cttt)
		{
			bool kq = true;
			long idphieuthanhtoan = 0;
			long iddotkham =0;
			long idkehoach=0;
			long idphieuthu = 0;
			try
			{
				BEGINTRANSACTION();
				//insert pheiu thanh toan 
				idphieuthanhtoan = INSERTHANHTOAN(tt);
				if (idphieuthanhtoan > 0)
				{
					foreach (CT_THANHTOAN ct in cttt)
					{
						CT_THANHTOAN mai = ct;
						mai.IDTT = idphieuthanhtoan;
						iddotkham=mai.IDDOTKHAM;
						idkehoach=mai.IDKEHOACH;
						if (!INSERTCT_THANHTOAN(mai))
						{
							kq = false;
							break;
						}
						else
						{ 
							CT_KHAMBENH kb = new CT_KHAMBENH();
							kb.IDCONGVIEC=mai.IDCONGVIEC;
							kb.IDDOTKHAM=iddotkham;
							if (!UPDATE_KHAMBENH(kb))
							{
								kq = false;
								break;
							}
						}
					}
					if (kq)
					{
						//tao 1 phieu thu 
						if (idkehoach > 0 && iddotkham > 0)
						{
							PHIEUTHU pt = new PHIEUTHU();
							pt.IDBENHNHAN = tt.IDBENHNHAN;
							pt.IDDOTKHAM = iddotkham;
							pt.IDKEHOACH = idkehoach;
							pt.IDLOAITT = 1;
							pt.IDNGUOINHAN = tt.IDNHANVIEN;
							pt.LYDOTHU = "Thu tiền dịch vụ";
							pt.NGAYTHU = DateTime.Now.Date;
							pt.SOTIEN = tt.THANHTIEN;
							pt.BANGCHU = tt.BANGCHU;
							idphieuthu = INSERT_PHIEUTHU(pt);
							if (idphieuthu>0)
							{
								// cap nhat tien coc
								TIENCOC tcoc = new TIENCOC();
								tcoc.IDBENHNHAN=tt.IDBENHNHAN;
								tcoc.IDDOTKHAM=iddotkham;
								kq = TRUCOC(tcoc);
								THANHTOAN ttoanup = new THANHTOAN();
								ttoanup.IDTT=idphieuthanhtoan;
								ttoanup.IDPHIEUTHU=idphieuthu;
								kq = UPDATE_IDPHIEUTHUTHANHTOAN(ttoanup);
							}
							else
							{
								kq = false;
							}
						}
						else
						{
							kq = false;
						}
					}
				}
				else
				{
					kq = false;
				}

				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}

			}
			catch 
			{
				ROLLBACK();
				return false; 
			}
			finally
			{
				this.CLOSE();
			}
			return kq;
		}
		public DataTable GETLICHSU(THANHTOAN tt)
		{
			dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 3));
				return dt;
			}
			catch { return null; }

		}
		public DataTable GETLICHSUBENHNHAN(THANHTOAN tt)
		{
			dt = new DataTable();
			try
			{

				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 5));
				return dt;
			}
			catch { return null; }

		}
		public bool DELETE_CT(CT_THANHTOAN ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_THANHTOAN", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 2)) == 1);
			}
			catch { return false; }
		}
		public bool DELETE_TT(THANHTOAN tt)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 4)) == 1);
			}
			catch { return false; }
		}
		public bool DELETE_PT(PHIEUTHU pt)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPPHIEUTHU", ConvertClassToArray.GetParamName(pt, null), ConvertClassToArray.GetValues(pt, 3)) == 1);
			}
			catch { return false; }
		}
		public bool DELETE_CV(CT_KHAMBENH kb)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_KHAMBENH", ConvertClassToArray.GetParamName(kb, null), ConvertClassToArray.GetValues(kb, 9)) == 1);
			}
			catch { return false; }
		}
		public bool CONGCOC(TIENCOC tc)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENCOC", ConvertClassToArray.GetParamName(tc, null), ConvertClassToArray.GetValues(tc, 4)) == 1);
			}
			catch { return false; }
		}
		public bool ISCOC(TIENCOC tc)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPTIENCOC", ConvertClassToArray.GetParamName(tc, null), ConvertClassToArray.GetValues(tc, 5)) == 1);
			}
			catch { return false; }
		}
		public bool DELETE(THANHTOAN tt, List<CT_KHAMBENH> ctkb)
		{
			bool kq = true;
			bool ok = true;
			long iddotkham =0;
			try
			{
				BEGINTRANSACTION();
				CT_THANHTOAN ct = new CT_THANHTOAN();
				ct.IDTT=tt.IDTT;
				ok = DELETE_CT(ct);
				if (ok)
				{
					//detete cha 
					ok = DELETE_TT(tt);
					if (ok)
					{
						PHIEUTHU pt = new PHIEUTHU();
						pt.IDPT = tt.IDPHIEUTHU;
						ok = DELETE_PT(pt);
						if (ok)
						{
							//cap nhat lai danh sach cong viec 
							foreach (CT_KHAMBENH ctk in ctkb)
							{
								iddotkham=ctk.IDDOTKHAM;
								if (!DELETE_CV(ctk))
								{
									kq = false;
									break;
								}

							}
							if (kq&&iddotkham>0)
							{
								TIENCOC tc = new TIENCOC();
								tc.IDBENHNHAN = tt.IDBENHNHAN;
								tc.IDDOTKHAM = iddotkham;
								if (ISCOC(tc))
								{
									if (!CONGCOC(tc))
									{
										kq = false;
									}
								}
							}
						}
						else
						{
							kq = false;
						}
					}
					else
					{
						kq = false;
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}

			}
			catch
			{
				ROLLBACK();
				return false;
			}
			finally
			{
				this.CLOSE();
			}
			return kq;
		}
	}
}
